<style>
.pure_table_extra { width: 1100px; margin-top: 5px; }
.pure_table_extra th { height: 25px; line-height: 25px; }
.pure_table_extra img { cursor: pointer; margin-right: 2px; }
.pure_table_extra td { vertical-align: middle; }
</style>

<?php 
if ($user->role != UT_ADMIN && $user->role != UT_FINANCE) {
  return;
}
$date = date('Y-m-d', time());
$student_id = Utils::get_param('id', 0);

$opts = new stdClass();
$opts->cd = 't.student_id = :v1';
$opts->cdv = array(':v1' => $student_id);
$invs = $orm->get('pb_colg_invoice', 't.id', 'DESC', NULL, NULL, $opts)->d;

$opts = new stdClass();
$opts->cd = 't.student_id = :v1';
$opts->cdv = array(':v1' => $student_id);
$pays = $orm->get('pb_colg_payment', 't.id', 'DESC', NULL, NULL, $opts)->d;
$pays = array();
?>
<span style="padding: 5px 10px; font-weight: bold;" id="finance_status" class="ui-corner-all"></span>
<div>&nbsp;</div>
<table class="pure-table pure_table_extra" cellpadding=0 cellspacing=0>
  <thead>
    <tr>
      <th style="width: 100px;" class="ui-widget-header">Invoice ID</th>
      <th style="width: 60px;" class="ui-widget-header">Date</th>
      <th style="width: 100px;"class="ui-widget-header">Particular</th>
      <th style="display: none; width: 50px;" class="ui-widget-header">Credit</th>
      <th style="width: 50px;" class="ui-widget-header">Debit</th>
      <th style="width: 50px;" class="ui-widget-header">Status</th>
      <th class="ui-widget-header">Note</th>
      <th style="width: 120px;" class="ui-widget-header">User</th>
      <th style="width: 40px;" class="ui-widget-header"></th>
    </tr>
  </thead>
  <tbody id="finance_body"></tbody>
</table>

<?php include '../include/colg/inc.inv.js.php'; ?>

<script type="text/javascript">
  /*<![CDATA[*/
  var __pturl = '<?php echo ColgUtils::get_pturl($app_data); ?>';
  var __invs = JSON.parse('<?php echo addslashes(json_encode($invs)); ?>');
  var __pays = JSON.parse('<?php echo addslashes(json_encode($pays)); ?>');
  var __inv_pay = [];
  
  $(function() {
    __inv_pay = __pays.concat(__invs);
    __inv_pay = __inv_pay.sort(function(a, b) {
      return b.date.localeCompare(a.date);
    });
  });
  
  function render_finance() {
    $('#finance_status').html('Balance: ' + accounting.formatMoney(__data[0].balance__));
    if (__data[0].balance__ < 0) {
      $('#finance_status').addClass('ui-state-error');
      $('#finance_status').removeClass('ui-state-highlight');
    } else {
      $('#finance_status').removeClass('ui-state-error');
      $('#finance_status').addClass('ui-state-highlight');
    }
    
    $('#finance_body').empty();
    for (var idx in __inv_pay) {
      var itm = __inv_pay[idx];
      
      $('#finance_body').append(
        '<tr class="' + ((idx % 2 == 0) ? 'pure-table-even' : 'pure-table-odd') + '">' + 
          '<td>' + ((itm.track_id != undefined) ? itm.track_id : '<span style="color: grey;">PAY ' + itm.invoice_track_id + '<span>') + '</td>' +
          '<td>' + itm.date + '</td>' +
          '<td title="' + itm.particular + '">' + Utils.get_short(itm.particular, 12) + '</td>' +
          '<td style="display: none;">' + ((itm.track_id == undefined) ? accounting.formatMoney(itm.amount) : '') + '</td>' +
          '<td>' + ((itm.track_id != undefined) ? accounting.formatMoney(itm.amount) : '') + '</td>' +
          '<td>' + ((itm.balance__ == undefined) ? '' : ((itm.balance__ != 0) ? ((itm.balance__ > 0) ? '<span style="color: green;">' + accounting.formatMoney(itm.balance__) + '</span>' : '<span style="color: red;">' + accounting.formatMoney(itm.balance__) + '</span>') : '<span style="color: black;">Paid</span>')) + '</td>' +
          '<td title="' + itm.comment + '">' + Utils.get_short(itm.comment, 50) + '</td>' +
          '<td>' + itm.user_title__ + '</td>' +
          '<td>' +
          ((itm.track_id == undefined) ? '' : '<span style="cursor: pointer; float: left;" class="ui-button-icon-primary ui-icon ui-icon-search" onclick="edit_inv(' + itm.id + ');"></span>') +
          ((itm.track_id == undefined) ? '' : '<span style="cursor: pointer; float: left;" class="ui-button-icon-primary ui-icon ui-icon-print" onclick="window.open(\'' + __pturl + '&tmpl=f_inv&id=' + itm.id + '\');"></span>') +
          '</td>' +
        '</tr>'
      );
    }
    if (__inv_pay.length == 0) {
      $('#finance_body').append('<tr class="pure-table-even"><td colspan=11>None</td></tr>');
    }
  };

  function edit_inv(id) {
    var obj = Utils.get_by_id(id, __invs);
    if (obj) {
      Ajax.request(
        __requrl + 'get_payments',
        'req=' + Utils.encodeURI(obj.id),
        function(rslt) {
          obj.__pays = rslt;
          $('#inv_dialog_inv').dialog({
            buttons: {
              'Close': function() { 
                $(this).dialog('close');
              }
            },
            width: 1200
          });
          ____inv(obj, { vo: 1 });
        },
        null
      );
    }
  };
  /*]]>*/
</script>